﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;
using UDS.Entity;

namespace UDS.SubModule.AssignRule
{
    /// <summary>
    /// RightList 的摘要说明。
    /// </summary>
    public partial class RightListPage : Page
    {
        protected EntityType DisplayType
        {
            get;
            private set;
        }

        protected int SrcID
        {
            get;
            private set;
        }

        protected int ClassID
        {
            get;
            private set;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            int value;
            if (!int.TryParse(Request.QueryString["SrcID"], out value))
            {
                value = 0;
            }
            SrcID = value;

            if (!int.TryParse(Request.QueryString["DisplayType"], out value))
            {
                value = (int)EntityType.Position;
            }
            DisplayType = (EntityType)value;

            if (!int.TryParse(Request.QueryString["ClassID"], out value))
            {
                value = 0;
            }
            ClassID = value;

            // 在此处放置用户代码以初始化页面
            if (!Page.IsPostBack)
            {
                FillRightList(SiteContext.Current.UserName, ClassID);
            }
        }

        private void FillRightList(string userName, int classId)
        {
            Act.Items.Clear();

            foreach (AssignRight right in AssignRights.GetRightList(userName, classId))
            {
                var listItem = new ListItem();
                listItem.Value = right.ID.ToString();
                if (string.IsNullOrEmpty(right.Description))
                {
                    listItem.Text = HttpUtility.HtmlEncode(right.Name);
                }
                else
                {
                    listItem.Text = HttpUtility.HtmlEncode(right.Name) + " <span class=\"description\">" + HttpUtility.HtmlEncode(right.Description) + "</span>";
                }

                Act.Items.Add(listItem);
            }
        }

        private void cmdOK_Click(object sender, EventArgs e)
        {
            bool bChecked = false;
            for (int i = 0; i < Act.Items.Count; i++)
            {
                if (Act.Items[i].Selected)
                {
                    AssignRights.AddRight(SrcID, ClassID, DisplayType, Int32.Parse(Act.Items[i].Value));
                    bChecked = true;
                }
            }

            if (bChecked == false)
            {
                Response.Write("<script laguage='javascript'>alert('请选择要添加的权限');</script>");
            }
            else
            {
                string url = "RightListView.aspx?ObjID=" + SrcID + "&DisplayType=" + (int)DisplayType;
                Response.Write("<script laguage='javascript'>parent.location='" + url + "';</script>");
            }
        }

        private void cmdReturn_Click(object sender, EventArgs e)
        {
            if (SrcID > 0)
            {
                string url = "RightListView.aspx?ObjID=" + SrcID + "&DisplayType=" + (int)DisplayType;
                Response.Write("<script laguage='javascript'>parent.location='" + url + "';</script>");
            }
            else
            {
                Response.Write("<script laguage='javascript'>history.back();</script>");
            }
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.cmdOK.Click += new System.EventHandler(this.cmdOK_Click);
            this.cmdReturn.Click += new System.EventHandler(this.cmdReturn_Click);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion
    }
}